<?php

class ContainerPlanApp extends MemberbaseApp
{
	function fileload(){
		$n=$_POST['n'];
		if(empty($n)){
			echo 'Invalid file type.';
			exit();
		}
		$targetFolder = '/data/uploads';

		$verifyToken = md5('unique_salt' . $_POST['timestamp']);
		$t=time();
		if (!empty($_FILES) && $_POST['token'] == $verifyToken) {
			$tempFile = $_FILES['Filedata']['tmp_name'];
			$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
				
			$targetFile = rtrim($targetPath,'/') . '/' . $t.'_'.$_FILES['Filedata']['name'];

			$fileTypes = array('jpg','jpeg','gif','png','txt','doc','xlsx','docx'); // File extensions
			$fileParts = pathinfo($_FILES['Filedata']['name']);

			if (in_array($fileParts['extension'],$fileTypes)) {
				move_uploaded_file($tempFile,iconv("UTF-8","gb2312",$targetFile));

				$sqldb=&sqldb();

				$sql="update T_TF_JBOXPLAN set FILE_PATH='".$targetFolder. '/' . $t.'_'.$_FILES['Filedata']['name']."' where VC_NO='".$n."'";
				$sqldb->query($sql);

				include_once ROOT_PATH.'/service/ftp.php';
				$ftp = new class_ftp('58.247.250.76',2121,'ftpglpex','web202!!#)'); // 打开FTP连接
				$ftp->up_file(iconv("UTF-8","gb2312",$targetFile),iconv("UTF-8","gb2312",'/Clp/'. $_FILES['Filedata']['name'])); // 上传文件
				$ftp->close();
				echo '1';
			} else {
				echo 'Invalid file type.';
			}
		}
	}
	
	function index()
	{
		$sqldb=&sqldb();

		$uid=$this->visitor->get("user_id");
		$uid=empty($uid)?0:intval($uid);

		$k = empty($_REQUEST['k']) ? '' : $_REQUEST['k'];

		$conditions = ' and 1=1';
		if($k){
			$conditions.=" and (C.SHIP_NO like '%".$k."%' or A.TD_NO like '%".$k."%'or A.BOX_NO like '%".$k."%')";
		}

		$db=&db();
		$sql="select depot,user_name from zys_member where user_id=".$uid;
		$u=$db->getRow($sql);

		$page = $this->_get_pagen(10);

		$sql="select * from
		(select x.*,ROW_NUMBER() OVER (ORDER BY x.JG_DD desc) AS RowNumber from
		(SELECT DISTINCT A.ET_NO VC_NO,dbo.PT_UBOXINFO(a.ztd_no,'CMHC','','','') SHIP_NO,A.TD_NO,A.XX,1 QTY,A.JP_DD JG_DD
,A.REM1 REM,A.CUS_NO,ET_NO,A.BOX_NO,A.CC,dbo.PT_UBOXINFO(a.ztd_no,'ZGDD','','','') ZGSJ from T_STOCK A 
left join T_UBOXPLAN c on A.TD_NO=c.td_no
where (c.USR='".$u['user_name']."' OR ISNULL(c.MK_USR,'')='".$u['user_name']."') and isnull(JG_FLAG,'')='Y' $conditions)x ) T_TF_JBOXPLAN where RowNumber > ".$page['s']." 
		AND RowNumber <= ".$page['e']." ORDER BY JG_DD desc";

		$plan=$sqldb->getAll($sql);
		$list=array();
		if($plan){
			foreach ($plan as $v){
				if($v['JG_DD']){
					$v['JG_DD']=gmstr2time($v['JG_DD']);
				}
				if($v['ZGSJ']){
					$v['ZGSJ']=gmstr2time($v['ZGSJ']);
				}
				$list[]=$v;
			}
		}

		$sql="select count(1) from (SELECT DISTINCT A.ET_NO VC_NO,dbo.PT_UBOXINFO(a.ztd_no,'CMHC','','','') SHIP_NO,A.TD_NO,A.XX,1 QTY,A.JP_DD JG_DD
,A.REM1 REM,A.CUS_NO,ET_NO,A.BOX_NO,A.CC,dbo.PT_UBOXINFO(a.ztd_no,'ZGDD','','','') ZGSJ from T_STOCK A 
left join T_UBOXPLAN c on A.TD_NO=c.td_no
where (c.USR='".$u['user_name']."' OR ISNULL(c.MK_USR,'')='".$u['user_name']."') and isnull(JG_FLAG,'')='Y' $conditions) x";
		$c=$sqldb->getOne($sql);
		//$this->json_result($list);

		$page['item_count'] = $c;
		$this->_format_pageweb($page);
		$this->assign('page_info', $page);

		$timestamp=time();
		$this->assign('timestamp', $timestamp);
		$this->assign('md5', md5('unique_salt' . $timestamp));
		$this->assign('plan', $list);
		$this->assign('d', time());
		$this->display('containerplan.html');
	}

	function getuse(){
		try{
			$sqldb=&sqldb();
			$db=&db();
			$v=!isset($_REQUEST['v'])?"":$_REQUEST['v'];

			if(!$v){
				throw new Exception("");
			}

			$sql="select BOX_NO,XX,CC,CUS_NO,SHIP_OVOY,TD_NO from T_Stock where BOX_NO='".$v."' and isnull(JG_FLAG,'')!='Y'";
			$s=$sqldb->getRow($sql);

			if(!$s){
				throw new Exception("");
			}

			$this->json_result($s);
		}catch (Exception $e){
			$this->json_error($e->getMessage());
		}
	}

	function jg(){
		$sqldb=&sqldb();
		$his= empty($_REQUEST['his']) ? '' : $_REQUEST['his'];
		if(empty($his)){
			$this->pons_error('info','您没有添加任何数据');
			exit();
		}
		$his=array_values($his);
		$uid=$this->visitor->get("user_id");

		$db=&db();
		$sql="select depot,del,company,user_name from zys_member where user_id=".$uid;
		$u=$db->getRow($sql);

		if($u['del']){
			$this->pons_error('info','用户被禁用，如有疑问请联系客服');
			exit();
		}

		if(!$u['depot']){
			$this->pons_error('info','用户还没有被系统确认，无法生成');
			exit();
		}

		$sql="select * from T_SALM where SAL_NO='".$u['depot']."'";
		$salm=$sqldb->getRow($sql);

		if(!$salm){
			$this->pons_error('info','用户还没有被系统确认，无法生成');
			exit();
		}

		foreach ($his as $vv){
			if(empty($vv['BOX_NO'])){
				continue;
			}

			$sql="select BOX_NO,XX,CC,CUS_NO,SHIP_OVOY,TD_NO,ET_NO from T_Stock where BOX_NO='".$vv['BOX_NO']."' and isnull(JG_FLAG,'')!='Y'";
			$v=$sqldb->getRow($sql);

			if(empty($v)){
				continue;
			}

			$BOX_NO= empty($v['BOX_NO']) ? '' : $v['BOX_NO'];
			$CUS_NO = empty($v['CUS_NO']) ? '' : $v['CUS_NO'];
			$XX= empty($v['XX']) ? '' : $v['XX'];
			$CC= empty($v['CC']) ? '' : $v['CC'];
			$SHIP_OVOY= empty($v['SHIP_OVOY']) ? '' : $v['SHIP_OVOY'];
			$TD_NO= empty($v['TD_NO']) ? '' : $v['TD_NO'];
			$ET_NO= empty($v['ET_NO']) ? '' : $v['ET_NO'];
			$JG_DD= empty($vv['JG_DD']) ? '' : $vv['JG_DD'];
			$REM= empty($vv['REM']) ? '' : $vv['REM'];
			if(!$JG_DD){
				$this->pons_error('info','请选择截港时间');
				exit();
			}

			/*$sql="insert into T_TF_JBOXPLAN(VC_NO,SHIP_NO,TD_NO,XX,QTY,JG_DD,REM,USR,COMP,COMP_NO,MK_DD,CUS_NO,ET_NO,BOX_NO,CC)
		values('".$this->create_guidnew()."','".$SHIP_OVOY."','".$TD_NO."','".$XX."','1','".$JG_DD."',
		'".$REM."','".$u['user_name']."','".$salm['COMP']."','".$salm['COMPNO']."',
		'".local_date('Y-m-d H:i:s',gmtime())."','".$CUS_NO."','".$ET_NO."','".$BOX_NO."','".$CC."')";
			$sqldb->query($sql);*/

			$sql="UPDATE T_STOCK SET JG_FLAG='Y',JP_DD='".$JG_DD."',REM1='".$REM."' WHERE BOX_NO='".$BOX_NO."'";
			$sqldb->query($sql);
		}

		$ret_url="index.php?app=containerplan";
		$this->pons_result($ret_url);
		exit();
	}
}

?>
